/*
 * Created on 15.06.2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package pho.prowon.index;

import java.io.InputStream;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import pho.helper.XMLHelper;

/**
 * @author Peter
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class IndexXMLImplementationDOM
	extends IndexXMLImplementation
	implements IndexImplementation
{
	// this is package private so IndexFactor can access it but it shouldn't be
	// accessed directly from outside
	IndexXMLImplementationDOM()
	{
	}

	protected List readIndex(InputStream is)
	{
		Document document;
		Element element;
		
		document = XMLHelper.loadDocumentFromStream(is);
		if (document != null)
		{
			element = document.getDocumentElement();
			NodeList nl = element.getElementsByTagName(getImageElement());
			for (int i = 0; i < nl.getLength(); i++)
			{
				currentTagContent.setLength(0);

				// ImageData nodes
				element = (Element) nl.item(i);

				// don't read the jpeg file, just parse the metadata from the index
				// unless checkIt is true, in this case we also check if the file exists
				// and forget about it if not...
				Node textNode =
					(element.getElementsByTagName("FilePath").item(0))
						.getFirstChild();
				if (textNode != null)
				{
					currentTagContent.append (textNode.getNodeValue());
				}

				processNewImage();

				if (image != null)
				{
					// if loadIt then ignore the index values...				
					if (!loadIt.booleanValue())
					{
						NodeList snl = element.getChildNodes();
						for (int j = 0; j < snl.getLength(); j++)
						{
							// IPTC tag nodes
							Element subelement = (Element) snl.item(j);
							String nodeName = subelement.getNodeName();
							if (!nodeName.equals("FilePath"))
							{
								currentTag = nodeName;
								
								Node child = (Node) subelement.getFirstChild();
								if (child != null)
								{
									currentTagContent.setLength(0);
									if (child.getNodeValue() == null)
									{										
										// so this might be an array
										NodeList valueList =
											subelement.getChildNodes();

										for (int k = 0;
											k < valueList.getLength();
											k++)
										{
											Node valueElement =
												(Node) valueList.item(k);
											Node valueChild =
												(Node) valueElement
													.getFirstChild();
											if (valueChild != null)
											{
												currentTagContent.setLength(0);
												currentTagContent.append(valueChild.getNodeValue());
												processValue();
											}
										}
									}
									else
									{
										currentTagContent.append(child.getNodeValue());
										processValue();
									}
								}
								processEndTag();
							}
						}
					}
					processEndImage();
				}
			}
		}

		return imageList;
	}

}
